$(document).ready(function () {
    $('#loginButton').unbind("click");
    $('#loginButton').click(function () {
        var email = $('#email').val();
        var password = $('#password').val();

        if (email == null || email == '') {
            alert("邮箱不能为空");
            return;
        }
        if(email.indexOf("@")<=0){
            alert("邮箱格式错误");
            return;
        }
        if (password == null || password == '') {
            alert("密码不能为空");
            return;
        }
        $.ajax({
            type: "POST",
            url: "login",
            dataType: "json",
            data: {
                'email': email,
                'password': password
            },
            timeout: 10000,
            success: function (res) {
                if (res.result == 'SUCCESS') {
                    window.location.href = 'index.html';
                }
                if (res.result == 'FAILED') {
                    alert(res.reason);
                }

            }

        });
    });

    $('#forgetPassword').unbind("click");
    $('#forgetPassword').click(function () {
        var email = $('#email').val();
        if (email == null || email == '') {
            alert("请先输入邮箱");
            return;
        }
        if(email.indexOf("@")<=0){
            alert("邮箱格式错误");
            return;
        }

        $.ajax({
            type: "POST",
            url: "forget",
            dataType: "json",
            data: {
                'email': email
            },
            timeout: 10000,
            success: function (res) {
                if (res.result == 'SUCCESS') {
                    alert("密码已经发送至您的邮箱");
                }
                if (res.result == 'WRONG'){
                    alert("此邮箱还没有注册");
                }
                if (res.result == 'FAILED') {
                    alert(res.reason);
                }

            }

        });
    });
});